import { KKCallback, KKRetriever, KKSize, UNIT_SIZE } from '@coldcloude/kakera';
import { _decorator, Component } from 'cc';
const { ccclass } = _decorator;

@ccclass('TileController')
export class TileController extends Component {
    //change
    changeHandler:KKCallback;
    change(){
        if(this.changeHandler){
            this.changeHandler();
        }
    }
    onChange(handler:KKCallback){
        this.changeHandler = handler;
    }
    //get visible
    visibleRetriever:KKRetriever<boolean>;
    isVisible():boolean{
        if(this.visibleRetriever){
            return this.visibleRetriever();
        }
    }
    onIsVisible(retriever:KKRetriever<boolean>){
        this.visibleRetriever = retriever;
    }
    //size
    sizeRetriever:KKRetriever<KKSize>;
    getSize():KKSize{
        if(this.sizeRetriever){
            const size = this.sizeRetriever();
            if(size.width<1&&size.width<1){
                return UNIT_SIZE;
            }
            else if(size.width<1){
                return {
                    width: 1,
                    height: size.height
                };
            }
            else if(size.height<1){
                return {
                    width: size.width,
                    height: 1
                };
            }
            else{
                return size;
            }
        }
        else{
            return UNIT_SIZE;
        }
    }
    onGetSize(retriever:KKRetriever<KKSize>){
        this.sizeRetriever = retriever;
    }
    //flag
    flagRetriever:KKRetriever<number>;
    getFlag():number{
        return this.flagRetriever?this.flagRetriever():0;
    }
    onGetFlag(retriever:KKRetriever<number>){
        this.flagRetriever = retriever;
    }
}
